"""   
   Copyright 2012 Stephen Jeapes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

import sys
write = sys.stdout.write

def bitpositions(strdata):
    binoffset = strdata.index('b')+ 1
    print "Bit Positions: "
    print "Bit# - Value"
    for i in range(0,len(strdata) - binoffset):
        print len(strdata) - binoffset - i, " - ", strdata[i+binoffset]   

""" Bitgrid function is currently unused. 
The idea is to provide a more compact display of bit positions than a simple list provides"""
def bitgrid(strdata):
    for i in range(0,16):
        write('-')
    for i in range(0,16):
        if (16-i)% 10 == 0:
            write((16-i)%10)
            
    write('\n')

print "Hex values should be entered as 0x<value> "
print "Decimal values with no pre-fix or suffix "
print "and binary values as 0b<value>, note this is Zero-b"

while 1:
    userint = input("Value to convert - ")
    userdict = dict(value = userint)
    
    print "Decimal Value: ", userint
    print "Hex Value: 0x%(value)X" % userdict
    print "Binary Value: ", bin(userint)
    bitpositions(bin(userint))
    #bitgrid(bin(userint))


